package com.ws.hw_student.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ws.hw_student.system.entity.SysUserRole;
import com.ws.hw_student.system.service.ISysUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/sysUserRole")
public class SysUserRoleController {

    @Autowired
    private ISysUserRoleService sysUserRoleService;

    /**
     * 分页查询用户角色关联
     */
    @GetMapping("/page")
    public Map<String, Object> page(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) Integer userId,
            @RequestParam(required = false) Integer roleId) {

        Page<SysUserRole> page = new Page<>(current, size);
        QueryWrapper<SysUserRole> queryWrapper = new QueryWrapper<>();

        if (userId != null) {
            queryWrapper.eq("user_id", userId);
        }
        if (roleId != null) {
            queryWrapper.eq("role_id", roleId);
        }

        IPage<SysUserRole> pageResult = sysUserRoleService.page(page, queryWrapper);

        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("data", pageResult);
        return result;
    }

    /**
     * 新增用户角色关联
     */
    @PostMapping("/add")
    public Map<String, Object> add(@RequestBody SysUserRole sysUserRole) {
        boolean saved = sysUserRoleService.save(sysUserRole);
        Map<String, Object> result = new HashMap<>();
        result.put("success", saved);
        result.put("message", saved ? "新增成功" : "新增失败");
        return result;
    }

    /**
     * 获取用户角色关联详情
     */
    @GetMapping("/detail/{id}")
    public Map<String, Object> detail(@PathVariable Integer id) {
        SysUserRole sysUserRole = sysUserRoleService.getById(id);
        Map<String, Object> result = new HashMap<>();
        result.put("success", sysUserRole != null);
        result.put("data", sysUserRole);
        if (sysUserRole == null) {
            result.put("message", "未找到对应的用户角色关联");
        }
        return result;
    }

    /**
     * 更新用户角色关联
     */
    @PutMapping("/update")
    public Map<String, Object> update(@RequestBody SysUserRole sysUserRole) {
        boolean updated = sysUserRoleService.updateById(sysUserRole);
        Map<String, Object> result = new HashMap<>();
        result.put("success", updated);
        result.put("message", updated ? "更新成功" : "更新失败");
        return result;
    }

    /**
     * 删除用户角色关联
     */
    @DeleteMapping("/delete/{id}")
    public Map<String, Object> delete(@PathVariable Integer id) {
        boolean removed = sysUserRoleService.removeById(id);
        Map<String, Object> result = new HashMap<>();
        result.put("success", removed);
        result.put("message", removed ? "删除成功" : "删除失败");
        return result;
    }

    /**
     * 根据用户ID查询角色关联
     */
    @GetMapping("/byUser/{userId}")
    public Map<String, Object> getByUserId(@PathVariable Integer userId) {
        QueryWrapper<SysUserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        return Map.of("success", true, "data", sysUserRoleService.list(queryWrapper));
    }

    /**
     * 根据角色ID查询用户关联
     */
    @GetMapping("/byRole/{roleId}")
    public Map<String, Object> getByRoleId(@PathVariable Integer roleId) {
        QueryWrapper<SysUserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", roleId);
        return Map.of("success", true, "data", sysUserRoleService.list(queryWrapper));
    }
}